<?php 
namespace app\admin\controller;
class Common {

	protected $view;

	function __construct() {
		if (!\mylib\Session::get('admin_login')) {
			\mylib\Response::redirect(\mylib\Route::buildUrl('/admin/auth/login'));
		}
		$this -> view = new \mylib\View(\mylib\Config::get('view'));
	}

	protected function assign($data=[], $value=null){
		$this -> view -> assign($data, $value);
		return $this -> view;
	}

	protected function fetch($tpl=null, $data=[]){
		return $this -> view -> fetch($tpl, $data);
	}

	protected function html($html=''){
		return \mylib\Response::html($html);
	}

	protected function success($message='操作成功！', $url='', $data=[]){
		if (\Ebcms::isAjax()) {
			return \mylib\Response::json([
				'code'=>1,
				'message'=>$message,
				'url'=>$url,
				'data'=>$data,
			]);
		}else{
			return $this -> html($this -> assign([
				'code'=>1,
				'message'=>$message,
				'url'=>$url,
				'data'=>$data,
			]) -> fetch('/common/notice'));
		}
	}

	protected function error($message='操作失败！', $url='', $data=[]){
		if (\Ebcms::isAjax()) {
			return \mylib\Response::json([
				'code'=>0,
				'message'=>$message,
				'url'=>$url,
				'data'=>$data,
			]);
		}else{
			return $this -> html($this -> assign([
				'code'=>0,
				'message'=>$message,
				'url'=>$url,
				'data'=>$data,
			]) -> fetch('/common/notice'));
		}
	}
}